const url_config = require('../../common/config');
var that;
var app = getApp();
var isRequesting = false; //是否正在请求数据的标记，下拉刷新和加载更多用的到
var currentPage = 0; //数据分页
let pageSize = 15; //每页的数据
Page({

  /**
   * 页面的初始数据
   */
  data: {
    'fileUrl': url_config.url_microFileService,
    rankerList: [],
    isScrollBackIconShow: false,
    noMoreData: false, //是否无更多数据 ，默认有更多数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
    currentPage = 0;
    that.getRankerList();
  },

  getRankerList: function() {
    if (isRequesting) {
      return;
    }
    isRequesting = true;
    wx.request({
      url: url_config.url_my_rankers,
      method: 'POST',
      data: {
        'pageNo': currentPage,
        'pageSize': pageSize
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'uidusernamekey': app.globalData.userInfo.token
      },
      success: function(res) {
        if (currentPage == 0) {
          //说明是第一页
          that.data.rankerList = [];
        }
        let resData = res.data;
        console.log(resData);
        let listData = resData.data;
        if (resData.status == 200 && listData && listData.length > 0) {
          if (currentPage == 0 && listData.length < pageSize) {
            that.data.noMoreData = true;
          } else {
            that.data.noMoreData = false;
          }

          for (let i = 0; i < listData.length; i++) {
            //追加到集合的末尾
            that.data.rankerList.push(listData[i]);
          }
        } else {
          that.data.noMoreData = true;
        }

        that.setData({
          rankerList: that.data.rankerList,
          noMoreData: that.data.noMoreData
        })
      },
      complete: function() {
        isRequesting = false;
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },
  // 显示时间格式,多长时间以前
  getTimeFormat: function(oldTime) {
    let result_time = '';
    oldTime = oldTime.length == 13 ? oldTime : oldTime * 1000;
    let temp = new Date().getTime() - oldTime;
    if (temp / 1000 < 1) {
      result_time = "刚刚";
    } else if (temp / 1000 < 60) {
      result_time = Math.round(temp / 1000) + "秒前";
    } else if (temp / 1000 < 3600) {
      result_time = Math.round(temp / 1000 / 60) + "分钟前";
    } else if (temp / 1000 < 86400) {
      result_time = Math.round(temp / 1000 / 3600) + "小时前";
    } else if (temp / 1000 < 604800) {
      result_time = Math.round(temp / 1000 / 86400) + "天前";
    } else if (temp / 1000 < 2419200) {
      result_time = Math.round(temp / 1000 / 604800) + "周前";
    } else {
      var date = new Date(oldTime);
      result_time = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
    }
    return result_time;
  },

  // 条目点击，跳转到提问详情页面
  showQdetailPage: function(e) {
    //得到这个条目的数据对象
    console.log(e);
    let current_question = that.data.rankerList[e.currentTarget.dataset.index];
    wx.navigateTo({
      url: '../qdetail/qdetail?current_question=' + JSON.stringify(current_question),
    })
  },

  // ----------------回到顶部

  srollBackToTop: function() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 500
    })
  },

  // -------------页面滑动的监听
  onPageScroll: function(res) {
    if (res.scrollTop > 200) {
      if (!that.data.isScrollBackIconShow) {
        that.setData({
          isScrollBackIconShow: true
        })
      }
    } else if (that.data.isScrollBackIconShow) {
      that.setData({
        isScrollBackIconShow: false
      })
    }
  },

  //--------------------下拉刷新------------
  onPullDownRefresh: function() {
    currentPage = 0;
    that.getrankerListList();
  },
  //-----------------------------加载更多
  onReachBottom: function() {
    if (isRequesting || that.data.noMoreData) {
      return;
    }
    currentPage++;
    that.getRankerList();
  },
})